package com.webreport.webapp.controller;

import java.util.List;





import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.webreport.exception.ManagerException;
import com.webreport.model.ReportModel;
import com.webreport.service.ReportManager;
import com.webreport.webapp.bean.PaginationFilter;
import com.webreport.webapp.bean.ReportListBean;

@Controller("reportController")
public class ReportController extends BaseController{	
	private ReportManager reportManager;
	@Autowired
	public void setReportManager(ReportManager reportManager) {
		this.reportManager = reportManager;
		this.marshaller.setClassesToBeBound(ReportListBean.class);
	}
	
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST}, value="/report/{menuid}")
	public ModelAndView getReports(@ModelAttribute PaginationFilter pf,@ModelAttribute ReportModel queryparams,@PathVariable String menuid) throws ManagerException {
		if (!StringUtils.isEmpty(menuid) && !StringUtils.isEmpty("report")){
			queryparams.setMenuid(menuid);
		}
		pf.setCount(this.reportManager.queryListCount(queryparams));
		List<ReportModel> reports = this.reportManager.queryList(queryparams, pf.getCurrentStartRow(), pf.getRowsPerPage());
		ReportListBean list = new ReportListBean(reports);
		list.setPagination(pf);
		list.setMenuid(menuid);
		if (StringUtils.equals("rec", menuid)){
			list.setTitle("收货");
		}else if (StringUtils.equals("inv", menuid)){
			list.setTitle("库存");
		}else{
			list.setTitle("发货");
		}
		return new ModelAndView("/report/reportlist", "reports", list);
	}
	@RequestMapping(method={RequestMethod.GET,RequestMethod.POST}, value="/report/reports")
	public ModelAndView getReports(@ModelAttribute PaginationFilter pf,@ModelAttribute ReportModel queryparams) throws ManagerException {
		return this.getReports(pf, queryparams, "");
	}
	
	
}
